package com.sunchangpeng.wechat.mp.kf;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.sunchangpeng.wechat.core.WechatClient;
import com.sunchangpeng.wechat.mp.EndpointProvider;
import com.sunchangpeng.wechat.mp.MpAppSetting;
import com.sunchangpeng.wechat.mp.MpWechatClientFactory;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;

public class KFMessages {
    private WechatClient wechatClient;

    public void setWechatClient(WechatClient wechatClient) {
        this.wechatClient = wechatClient;
    }

    public static KFMessages with(MpAppSetting appSetting) {
        KFMessages kfMessages = new KFMessages();
        kfMessages.setWechatClient(MpWechatClientFactory.getInstance().with(appSetting));
        return kfMessages;
    }

    /**
     * 客服发送文本消息
     */
    public void text(String openId, String text) {
        text(openId, text, null);
    }

    /**
     * 客服发送文本消息
     */
    public void text(String openId, String text, String from) {
        JSONObject msg = initMessage(openId, "text", from);
        msg.put("text", new JSONObject().fluentPut("content", text));
        String url = EndpointProvider.get("url.care.message.send");
        wechatClient.getTextWithToken(url, JSON.toJSONString(msg));
    }

    /**
     * 客服发送图片消息
     *
     * @param openId
     * @param image
     */
    public void image(String openId, String image) {
        image(openId, image, null);
    }

    /**
     * 客服发送图片消息
     */
    public void image(String openId, String image, String from) {
        JSONObject msg = initMessage(openId, "image", from);
        msg.put("image", new JSONObject().fluentPut("media_id", image));
        String url = EndpointProvider.get("url.care.message.send");
        wechatClient.getTextWithToken(url, JSON.toJSONString(msg));
    }

    /**
     * 客服发送语音消息
     */
    public void voice(String openId, String voice) {
        voice(openId, voice, null);
    }

    /**
     * 客服发送语音消息
     */
    public void voice(String openId, String voice, String from) {
        JSONObject msg = initMessage(openId, "voice", from);
        msg.put("voice", new JSONObject().fluentPut("media_id", voice));
        String url = EndpointProvider.get("url.care.message.send");
        wechatClient.getTextWithToken(url, JSON.toJSONString(msg));
    }

    /**
     * 客服发送video消息
     */
    public void video(String openId, Video video) {
        video(openId, video, null);
    }

    /**
     * 客服发送video消息
     */
    public void video(String openId, Video video, String from) {
        JSONObject msg = initMessage(openId, "video", from);
        msg.put("video", video);
        String url = EndpointProvider.get("url.care.message.send");
        wechatClient.getTextWithToken(url, JSON.toJSONString(msg));
    }

    /**
     * 客服发送music消息
     */
    public void music(String openId, Music music) {
        music(openId, music, null);
    }

    /**
     * 客服发送music消息
     */
    public void music(String openId, Music music, String from) {
        JSONObject msg = initMessage(openId, "music", from);
        msg.put("music", music);
        String url = EndpointProvider.get("url.care.message.send");
        wechatClient.getTextWithToken(url, JSON.toJSONString(msg));
    }

    /**
     * 客服发送mpNews消息
     */
    public void mpNews(String openId, String mpNews) {
        mpNews(openId, mpNews, null);
    }

    /**
     * 客服发送mpNews消息
     */
    public void mpNews(String openId, String mpNews, String from) {
        JSONObject msg = initMessage(openId, "mpnews", from);
        msg.put("mpnews", new JSONObject().fluentPut("media_id", mpNews));
        String url = EndpointProvider.get("url.care.message.send");
        wechatClient.getTextWithToken(url, JSON.toJSONString(msg));
    }

    /**
     * 客服发送news消息
     */
    public void news(String openId, News news) {
        news(openId, news, null);
    }

    /**
     * 客服发送news消息
     */
    public void news(String openId, News news, String from) {
        JSONObject msg = initMessage(openId, "news", from);
        msg.put("news", new JSONObject().fluentPut("articles", Arrays.asList(news)));
        String url = EndpointProvider.get("url.care.message.send");
        wechatClient.getTextWithToken(url, JSON.toJSONString(msg));
    }

    /**
     * 发送卡券
     */
    public void wxCard(String openId, String cardId) {
        wxCard(openId, cardId, null);
    }

    /**
     * 发送卡券
     */
    public void wxCard(String openId, String cardId, String from) {
        JSONObject msg = initMessage(openId, "wxcard", from);
        msg.put("wxcard", new JSONObject().fluentPut("card_id", cardId));
        String url = EndpointProvider.get("url.care.message.send");
        wechatClient.getTextWithToken(url, JSON.toJSONString(msg));
    }

    /**
     * 发送小程序卡片
     */
    public void miniProgramPage(String openId, MiniProgram miniProgram) {
        miniProgramPage(openId, miniProgram, null);
    }

    /**
     * 发送小程序卡片
     */
    public void miniProgramPage(String openId, MiniProgram miniProgram, String from) {
        JSONObject msg = initMessage(openId, "miniprogrampage", from);
        msg.put("miniprogrampage", miniProgram);
        String url = EndpointProvider.get("url.care.message.send");
        wechatClient.getTextWithToken(url, JSON.toJSONString(msg));
    }

    private JSONObject initMessage(String openId, String msgType, String from) {
        JSONObject msg = new JSONObject().fluentPut("msgtype", msgType).fluentPut("touser", openId);
        if (StringUtils.isNotBlank(from)) {
            msg.put("customservice", new JSONObject().fluentPut("kf_account", from));
        }
        return msg;
    }

    /**
     * 客服输入状态
     */
    public void typing(String openId) {
        String url = EndpointProvider.get("url.care.message.typing");
        wechatClient.getTextWithToken(url, JSON.toJSONString(new JSONObject()
                .fluentPut("touser", openId)
                .fluentPut("command", "Typing")));
    }

    /**
     * 取消客服输入状态
     */
    public void cancelTyping(String openId) {
        String url = EndpointProvider.get("url.care.message.typing");
        wechatClient.getTextWithToken(url, JSON.toJSONString(new JSONObject()
                .fluentPut("touser", openId)
                .fluentPut("command", "cancelTyping")));
    }

    @Data
    public static class Video {
        @JSONField(name = "media_id")
        private String mediaId;
        @JSONField(name = "thumb_media_id")
        private String thumbMediaId;
        private String title;
        private String description;
    }

    @Data
    public static class Music {
        @JSONField(name = "musicurl")
        private String musicUrl;
        @JSONField(name = "hqmusicurl")
        private String hqMusicUrl;
        @JSONField(name = "thumb_media_id")
        private String thumbMediaId;
        private String title;
        private String description;
    }

    @Data
    public static class News {
        private String title;
        private String description;
        @JSONField(name = "picurl")
        private String picUrl;
        private String url;
    }

    @Data
    public static class MiniProgram {
        private String title;
        @JSONField(name = "appid")
        private String appId;
        @JSONField(name = "pagepath")
        private String pagePath;
        @JSONField(name = "thumb_media_id")
        private String thumbMediaId;
    }
}
